var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");
var userName = sessionStorage.getItem("userName");
$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "../login.html";
//		top.location.href = "login.html";
	} else {
		var isaudit = 0;
		var pageNumber = "1";
		var pageSize = "10";
		var startday = "";
		var endday = "";
		var selectinfo = "";
		var reque = function() {
			return {
				pageSize: pageSize,
				pageNumber: pageNumber,
				isaudit: isaudit,
				startday: startday,
				endday: endday,
				selectinfo: selectinfo,

			}
		}
		rq1 = reque();
		var req = function() {
			return {
				requestString: "",
				userId: userId,
				fullname: fullname
			}
		}
		requestObj1 = req();
		requestObj1.requestString = JSON.stringify(rq1);
		var req3 = JSON.stringify(requestObj1);
		ajax_data(req3)
	}

});

function ajax_data(data) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectOrgainzeSettleList',
		data: data,
		async: false,
		dataType: "json",
		success: function(msg) {
//			console.log(msg);
			if(msg.responseCode == 200) {
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: 1,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}

$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "10";
	var isaudit = $("#select_type").val();
	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var selectinfo = $("#text_sousuo").val();

	//	var userId = "";
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			isaudit: isaudit,
			startday: star_time,
			endday: end_time,
			selectinfo: selectinfo,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectOrgainzeSettleList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
//			console.log(msg);
			if(msg.responseCode == 200) {
//				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {
//		console.log(bb);
		var bb = JSON.parse(bb);
		var tableStr = "";
		var len = bb.length;
		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				//				0 未支付 1 已支付 2 已取消
				//				3 已退款 4 缴费失败， 已退款
				if(bb[i].auditusername == undefined) {
					bb[i].auditusername = '';
				}
				if(bb[i].summary == undefined) {
					bb[i].summary = '';
				}
				if(bb[i].auditdate == undefined) {
					bb[i].auditdate = '';
				}
				if(bb[i].paymentamt == undefined) {
					bb[i].paymentamt = '';
				}
				if(bb[i].chargeway == 0) {
					bb[i].chargeway = "app";
				} else {
					bb[i].chargeway = "物业后台";
				}
				if(bb[i].settleway == 1) {
					bb[i].settleway = "系统自动结算";
				} else if(bb[i].settleway == 2) {
					bb[i].settleway = "人工结算";
				}
				switch(bb[i].isaudit) {
					case 0:
						bb[i].isaudit = "未审核";
						break;
					case 1:
						bb[i].isaudit = "审核通过";
						break;
					case 2:
						bb[i].isaudit = "审核不通过";
						break;

				}
				if(bb[i].apipayid == undefined) {
					bb[i].apipayid = ""
				}
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb[i].billid + '</th>' +
					'<th>' + bb[i].billcode + '</th>' +
					'<th>' + bb[i].payenddate + '</th>' +
					'<th>' + bb[i].settleqty + '</th>' +
					'<th>' + bb[i].settleamt + '</th>' +
					'<th>' + bb[i].settleway + '</th>' +
					'<th >' + bb[i].isaudit + '</th>' +

					'<th style="display:none" >' + bb[i].billdate + '</th>' +
					'<th  style="display:none" >' + bb[i].auditdate + '</th>' +
					'<th  style="display:none" >' + bb[i].auditusername + '</th>' +
					'<th  style="display:none" >' + bb[i].weixinsettleqty + '</th>' +
					'<th  style="display:none" >' + bb[i].weixinsettleamt + '</th>' +
					'<th  style="display:none" >' + bb[i].alisettleqty + '</th>' +
					'<th  style="display:none" >' + bb[i].alisettleamt + '</th>' +
					'<th  style="display:none" >' + bb[i].summary + '</th>' +
					'<th  style="color:blue;cursor: pointer" ;><span class="glyphicon glyphicon-edit"></span></th>' +
					//					'<th  style="color:red;cursor: pointer" ;><span class="glyphicon glyphicon-time"></span></th>' +

					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);

		}
		//点击详情
		var billid = "";
		$("#tbbb tr th:nth-child(17)").click(function() {
			billid = $(this).siblings().eq(1).html();
//			console.log(billid)
			$('#myModal').modal('show');
			$("#s_djrq").html($(this).siblings().eq(8).html());
			$("#s_shsj").html($(this).siblings().eq(9).html());
			$("#s_shr").html($(this).siblings().eq(10).html());
			$("#s_wxjsts").html($(this).siblings().eq(11).html());
			$("#s_wxjsje").html($(this).siblings().eq(12).html());
			$("#s_zfbjsts").html($(this).siblings().eq(13).html());
			$("#s_zfbjsje").html($(this).siblings().eq(14).html());
			$("#s_zy").html($(this).siblings().eq(15).html());
		});
		//审核通过
		$(".sh_wy_pass").click(function() {
//			console.log(billid)
			var data = JSON.stringify({
				userId: userId,
				fullname: fullname,
				requestString: JSON.stringify({
					billid: billid,
					audituser: userId,
					auditusername: userName,
				})
			});
			$.ajax({
				contentType: "application/json; charset=utf-8",
				type: "POST",
				url: urltest + 'verify/financial/updateSettlementOrderPass',
				data: data,
				async: true,
				dataType: "json",
				success: function(msg) {
//					console.log(msg);
					if(msg.responseCode == 200) {
						alert("审核通过");
						$('#myModal').modal('hide');
					} else {
						alert(msg.resoponseDisp)
					}
				},
				error: function() {
					alert("获取信息失败");
				}
			});
		});
		//审核不通过
		$(".sh_wy_nopass").click(function() {
			var data = JSON.stringify({
				userId: userId,
				fullname: fullname,
				requestString: JSON.stringify({
					billid: billid,
					audituser: userId,
					auditusername: userName,
				})
			});
			$.ajax({
				contentType: "application/json; charset=utf-8",
				type: "POST",
				url: urltest + 'verify/financial/updateSettlementOrderNotPass',
				data: data,
				async: true,
				dataType: "json",
				success: function(msg) {
//					console.log(msg);
					if(msg.responseCode == 200) {
						alert("审核不通过");
						$('#myModal').modal('hide');
						Window.location.href = "Property_settlement_audit.html"
					} else {
						alert(msg.resoponseDisp)
					}
				},
				error: function() {
					alert("获取信息失败");
				}
			});
		});
	}
}